﻿//当前页面类
function Hfzf() {
    var _aryFindValue = [];
    var _iTabIndex;
    var _me = this;
    //初始化标签页列表是否修改数据数组
    var _aryUpdateGrid = [false, false, false, false, false, false];
    var _initXsglHfzf = function() {
        //为起始日期和截止日期赋默认值
        var sCurDate = document.getElementById("hidCurDate").value;
        document.getElementById("txtEndDate").setValue(sCurDate);
        document.getElementById("txtBeginDate").setValue(sCurDate.substring(0, 8) + "01");
        //初始化置业顾问
        _me.initZygw();
    };
    
    //显示销售经理单选按钮
    var _showXsglRadio = function() {
    
        if (document.getElementById("hidIsSaleManager").value === "1") {
            var radXsgl = document.getElementById("radXsgl");
            radXsgl.style.visibility = "visible";
            radXsgl.nextSibling.style.visibility = "visible";
            var radZygw = document.getElementById("radZygw");
            radZygw.style.visibility = "visible";
            radZygw.nextSibling.style.visibility = "visible";
        }
    }

    //返回标签页查询区域值数组
    this.getFindValue = function() {
        return _aryFindValue;
    };
    
    

    //切换标签页
    this.changeTab = function(sTagName, spnTab) {

        //iframe对象、地址、tab标签对象、查找区域TABLE、查找区域行
        var ifmGrid, sFile, divTab, tblFind, rowFind1, rowFind2, oProjGUID, cboZygw;
        oProjGUID = document.getElementById("appProjTree");

        oProjGUID.isChangeTab = true;

        //上一个标签页的值
        var oTabFind = _aryFindValue[_iTabIndex];

        //保存当前标签页索引
        _iTabIndex = spnTab ? spnTab.tabIndex : event.srcElement.tabIndex;

        //得到上次当前标签页查询区域的值
        var oOldTabFind;
        oOldTabFind = _aryFindValue[spnTab ? spnTab.tabIndex : event.srcElement.tabIndex];

        oTabFind.ProjGUID = oProjGUID.value;

        oProjGUID.value = oOldTabFind.ProjGUID;
        sFile = "Hfzf_Pub_Grid.aspx";
        sFile += "?funcid=" + __funcid.value;
        oProjGUID.ovalue = oProjGUID.value;
        var btnSearch = document.getElementById("btnSearch");

        switch (sTagName) {
            case "Zygw_Dgj":
                divTab = document.getElementById("tab0");
                btnSearch.style.display = "none";
                break;
            case "Zygw_Jrxgj":
                divTab = document.getElementById("tab1");
                btnSearch.style.display = "none";
                break;
            case "Zygw_Yqwgj":
                divTab = document.getElementById("tab2");
                btnSearch.style.display = "none";
                break;
            case "Xsgl_Hfzf":
                sFile = "Hfzf_Xsgl_Hfzf.aspx";
                sFile += "?funcid=" + __funcid.value;
                sFile += "&begindate=" + document.getElementById("txtBeginDate").value;
                sFile += "&enddate=" + document.getElementById("txtEndDate").value;
                divTab = document.getElementById("tab3");
                btnSearch.style.display = "";
                break;
            case "Xsgl_Jrxgj":
                divTab = document.getElementById("tab4");
                cboZygw = document.getElementById("cboZygw");
                oTabFind.ZygwGUID = cboZygw.returnValue;
                cboZygw.setValue(oOldTabFind.ZygwGUID);
                btnSearch.style.display = "";
                sFile += "&userguid=" + document.getElementById("cboZygw").returnValue;

                break;
            case "Xsgl_Yqwgj":
                divTab = document.getElementById("tab5");
                cboZygw = document.getElementById("cboZygw");
                oTabFind.ZygwGUID = cboZygw.returnValue;
                cboZygw.setValue(oOldTabFind.ZygwGUID);
                btnSearch.style.display = "";
                sFile += "&userguid=" + document.getElementById("cboZygw").returnValue;

                break;
            default:
                divTab = document.getElementById("tab0");
                cboZygw = document.getElementById("cboZygw");
                oTabFind.ZygwGUID = cboZygw.returnValue;
                cboZygw.setValue(oOldTabFind.ZygwGUID);
                btnSearch.style.display = "";
                break;
        }

        sFile += "&viewtype=" + sTagName;
        sFile += "&projguid=" + oProjGUID.value;
        sFile += "&helpkey=" + sTagName;

        //加载iframe
        ifmGrid = divTab.getElementsByTagName("IFRAME")[0];
        //如果有修改过数据则重新刷新列表
        if (_aryUpdateGrid[_iTabIndex] || ifmGrid.src === "" || ifmGrid.src === "about:blank") {
            ifmGrid.src = sFile;
            _aryUpdateGrid[_iTabIndex] = false;
        }

        //查询区域处理
        tblFind = document.getElementById("tblFind");
        rowFind = tblFind.rows[0];
        //移动查询区域
        document.getElementById("trFind").swapNode(divTab.getElementsByTagName("TR")[0]);
        var tdDate = document.getElementById("tdDate");
        var tdZygw = document.getElementById("tdZygw");
        var tdZygwControl = document.getElementById("tdZygwControl");
        //显示隐藏查询区域的TD
        if (sTagName === "Xsgl_Hfzf") {
            //显示起始、截止日期
            //            rowFind.children[0].style.display = "";
            //            rowFind.children[1].style.display = "";
            //            rowFind.children[2].style.display = "";
            //            rowFind.children[3].style.display = "";

            tdDate.style.display = "";
            tdDate.style.visibility = "visible";
            //隐藏置业顾问下拉框
            tdZygw.style.display = "none";
            //            rowFind1.children[2].style.visibility = "hidden";
            //            rowFind1.children[3].style.visibility = "hidden";
        } else if (sTagName.substring(0, 4) === "Zygw") {
            //隐藏置业顾问下拉框
            //            rowFind1.children[2].style.visibility = "hidden";
            //            rowFind1.children[3].style.visibility = "hidden";
            tdZygw.style.display = "none";
            //隐藏起始、截止日期
            //            rowFind2.children[0].style.display = "none";
            //            rowFind2.children[1].style.display = "none";
            //            rowFind2.children[2].style.display = "none";
            //            rowFind2.children[3].style.display = "none";
            tdDate.style.visibility = "hidden";
            tdDate.style.display = "";
        } else {
            //显示置业顾问下拉框
            //            rowFind1.children[2].style.visibility = "visible";
            //            rowFind1.children[3].style.visibility = "visible";
            tdZygw.style.display = "";
            //隐藏起始、截止日期
            //            rowFind2.children[0].style.display = "none";
            //            rowFind2.children[1].style.display = "none";
            //            rowFind2.children[2].style.display = "none";
            //            rowFind2.children[3].style.display = "none";
            tdDate.style.display = "none";
        }


        oProjGUID.isChangeTab = false;
    };

    //切换视图
    this.showTabByView = function(sView, spnTab) {

        var tabs = document.getElementById("tabs").childNodes;
        var spnXsgl_Hfzf = document.getElementById("spnXsgl_Hfzf");
        var spnXsgl_tab, spnZygw_tab;
        if (sView === "Xsgl") {
            //显示销售经理标签页
            spnXsgl_Hfzf.style.display = "";
            spnXsgl_tab = spnXsgl_Hfzf.nextSibling;
            while (spnXsgl_tab) {
                if (spnXsgl_tab.tagName === "SPAN") {
                    spnXsgl_tab.style.display = "";
                }
                spnXsgl_tab = spnXsgl_tab.nextSibling;
            }
            //隐藏置业顾问标签页
            spnZygw_tab = spnXsgl_Hfzf.previousSibling;
            while (spnZygw_tab) {
                if (spnZygw_tab.tagName === "SPAN") {
                    spnZygw_tab.style.display = "none";
                }
                spnZygw_tab = spnZygw_tab.previousSibling;
            }

            spnXsgl_Hfzf.parentNode.parentNode.parentNode.parentNode.showTab(spnTab.tabIndex);
            spnTab.fireEvent("onclick");

        } else {
            //显示置业顾问标签页
            spnZygw_tab = spnXsgl_Hfzf.previousSibling;
            while (spnZygw_tab) {
                if (spnZygw_tab.tagName === "SPAN") {
                    spnZygw_tab.style.display = "";
                }
                spnZygw_tab = spnZygw_tab.previousSibling;
            }
            //隐藏销售经理标签页
            spnXsgl_Hfzf.style.display = "none";
            spnXsgl_tab = spnXsgl_Hfzf.nextSibling;
            while (spnXsgl_tab) {
                if (spnXsgl_tab.tagName === "SPAN") {
                    spnXsgl_tab.style.display = "none";
                }
                spnXsgl_tab = spnXsgl_tab.nextSibling;
            }

            spnXsgl_Hfzf.parentNode.parentNode.parentNode.parentNode.showTab(spnTab.tabIndex);
            spnTab.fireEvent("onclick");
        }
    };


    //根据项目不同加载相应的置业顾问
    this.initZygw = function() {

        var sUserGUID = document.getElementById("hidUserGUID").value;
        var sProjGUID = appProjTree.value;
        var sPath = "/Slxt/XSZDH/Yqywcb_Pub_XMLHTTP.aspx?UserGUID=" + sUserGUID + "&ProjGUID=" + sProjGUID;
        var strRtn = openXMLHTTP(sPath, "BuildZYGW");

        document.getElementById("_Preferred_Table").outerHTML = strRtn;
        document.getElementById("cboZygw").setValue("all");
    };

    //得到当前选中的标签页对象
    this.getOnTab = function() {
        
        var tabs = document.getElementById("tabs").childNodes;
        var spnTab;
        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].tagName === "SPAN" && tabs[i].className === "tab tabOn") {
                spnTab = tabs[i];
                break;
            }
        }
        return spnTab;
    }   
    
    //项目控件切换验证日期
    this.beforeDoQuery = function() {
        //得到当前选中的标签页
        var spnTab;
        spnTab = _me.getOnTab();
        if (spnTab.id.toLocaleUpperCase() === "spnXsgl_Hfzf".toLocaleUpperCase()) {
            var sBeginDate = document.getElementById("txtBeginDate").value;
            var sEndDate = document.getElementById("txtEndDate").value;
            if (sBeginDate && sEndDate) {
                if (sBeginDate > sEndDate) {
                    alert("截止日期要大于等于起始日期！");
                    event.returnValue = false;
                    return false;
                }
            }
        }
    }
    
    //设置列表更新标识
    this.setUpdateGrid = function() {
        for (var i = _aryUpdateGrid.length; i >= 0; i--) {
            if (i != _iTabIndex) {
                _aryUpdateGrid[i] = true;
            }
        }
    }   
    
    
    //查询事件
    this.doQuery = function(isProj, element) {

        var oProjGUID = document.getElementById("appProjTree");

        if ((element && element.isChangeTab == true) || (event.srcElement && event.srcElement.isChangeTab == true)) {
            return;
        }

        if (isProj && oProjGUID.value !== oProjGUID.ovalue) {
            _me.initZygw();
        }

        //得到当前选中的标签页
        var spnTab;
        spnTab = _me.getOnTab();
        if (spnTab) {
            var tblFind = document.getElementById("tblFind");
            var sParm, sFile, ifmGrid;
            sParm = "?funcid=" + __funcid.value;
            sParm += "&projguid=" + oProjGUID.value;
            sFile = "/Slxt/XSZDH/Hfzf_Pub_Grid.aspx";
            ifmGrid = document.getElementById("tab" + spnTab.tabIndex).getElementsByTagName("IFRAME")[0];
            switch (spnTab.id) {
                case "spnZygw_Dgj":           //置业顾问-客户分配后待跟进客户
                    sParm += "&viewtype=Zygw_Dgj";
                    break;
                case "spnZygw_Jrxgj":           //置业顾问-今日需跟进客户
                    sParm += "&viewtype=Zygw_Jrxgj";
                    break;
                case "spnZygw_Yqwgj":           //置业顾问-逾期未跟进客户
                    sParm += "&viewtype=Zygw_Yqwgj";
                    break;
                case "spnXsgl_Hfzf":           //销售经理-回访追记跟进情况
                    var sBeginDate = document.getElementById("txtBeginDate").value;
                    var sEndDate = document.getElementById("txtEndDate").value;
                    if (sBeginDate && sEndDate) {
                        if (sBeginDate > sEndDate) {
                            alert("截止日期要大于等于起始日期！");
                            return;
                        }
                    }
                    sFile = "/Slxt/XSZDH/Hfzf_Xsgl_Hfzf.aspx";
                    sParm += "&begindate=" + sBeginDate;
                    sParm += "&enddate=" + sEndDate;
                    break;
                case "spnXsgl_Jrxgj":           //销售经理-今日需跟进客户
                    sParm += "&viewtype=Xsgl_Jrxgj";
                    sParm += "&userguid=" + document.getElementById("cboZygw").returnValue;

                    break;
                case "spnXsgl_Yqwgj":           //销售经理-逾期未跟进客户
                    sParm += "&viewtype=Xsgl_Yqwgj";
                    sParm += "&userguid=" + document.getElementById("cboZygw").returnValue;

                    break;
                default:
                    sParm += "&viewtype=Zygw_Dgj";
                    break;
            }
            if (ifmGrid) {
                ifmGrid.contentWindow.location = sFile + sParm;
            }
        }
        oProjGUID.ovalue = oProjGUID.value;
    };

    function show(oShow, o) {
        try {
            with (oShow.style) {
                left = o.offsetLeft + 1;
                top = o.offsetTop - 4;
                width = o.offsetWidth - 2;
                display = "inline";
            }
        }
        catch (e)
	    { }
    };


    //构造函数初始化
    (function() {

        Hfzf = _me;
        var sProjGUID = document.getElementById("appProjTree").value;
        var sZygwGUID = "all";
        //保存标签页的查询状态
        _aryFindValue.push({ ProjGUID: sProjGUID });
        _aryFindValue.push({ ProjGUID: sProjGUID });
        _aryFindValue.push({ ProjGUID: sProjGUID });
        _aryFindValue.push({ ProjGUID: sProjGUID, ZygwGUID: sZygwGUID });
        _aryFindValue.push({ ProjGUID: sProjGUID, ZygwGUID: sZygwGUID });
        _aryFindValue.push({ ProjGUID: sProjGUID, ZygwGUID: sZygwGUID });


        //当前标签页索引
        _iTabIndex = 0;
        var sIsSaleManager = document.getElementById("hidIsSaleManager").value;
        var ifmGrid;
        var spnZygw_Dgj = document.getElementById("spnZygw_Dgj");
        var spnXsgl_Hfzf = document.getElementById("spnXsgl_Hfzf");
        if (sIsSaleManager === "1") {
            _initXsglHfzf();
            _showXsglRadio();
        }
        //判断是否是桌面部件进来
        var sViewtype = document.getElementById("hidviewtype").value;

        if (sViewtype) {

            sViewtype = sViewtype.toLocaleUpperCase()
            //判断当前用户是否有销售经理
            if (sIsSaleManager !== "1") {
                sViewtype = sViewtype.replace(/XSGL/gi, "ZYGW");
            }
            switch (sViewtype) {
                case "Zygw_Dgj".toLocaleUpperCase():
                    // 初始化设置首个TAB标签页的的地址
                    var spnZygw_Dgj = document.getElementById("spnZygw_Dgj");
                    //调整标签样式
                    show(document.getElementById("hrSelectedTab"), spnZygw_Dgj);
                    document.getElementById("radZygw").checked = true;
                    _me.changeTab("Zygw_Dgj", spnZygw_Dgj);
                    break;
                case "Zygw_Jrxgj".toLocaleUpperCase():
                    _iTabIndex = 1;
                    _me.showTabByView("Zygw", document.getElementById("spnZygw_Jrxgj"));
                    document.getElementById("radZygw").checked = true;
                    break;
                case "Zygw_Yqwgj".toLocaleUpperCase():
                    _iTabIndex = 2;
                    _me.showTabByView("Zygw", document.getElementById("spnZygw_Yqwgj"));
                    document.getElementById("radZygw").checked = true;
                    break;
                case "Xsgl_Jrxgj".toLocaleUpperCase():
                    _initXsglHfzf();
                    _iTabIndex = 4;
                    _me.showTabByView("Xsgl", document.getElementById("spnXsgl_Jrxgj"));
                    _showXsglRadio();
                    break;
                case "Xsgl_Yqwgj".toLocaleUpperCase():
                    _initXsglHfzf();
                    _iTabIndex = 5;
                    _me.showTabByView("Xsgl", document.getElementById("spnXsgl_Yqwgj"));
                    _showXsglRadio();
                    break;
            }

        } else {

            //如果销售经理单选按钮存在，则默认选择销售经理按钮，并显示回访追访标签页
            if (sIsSaleManager === "1") {
                //显示销售经理标签页,隐藏置业顾问标签页
                _me.showTabByView("Xsgl", document.getElementById("spnXsgl_Hfzf"));
                _iTabIndex = 3;
            } else {
                var spnZygw_Dgj = document.getElementById("spnZygw_Dgj");
                //调整标签样式
                show(document.getElementById("hrSelectedTab"), spnZygw_Dgj);
                // 初始化设置首个TAB标签页的的地址
                _me.changeTab("Zygw_Dgj", spnZygw_Dgj);
            }
        }

    })();


    
    
}
